package com.chat;


import com.chat.mapper.UserMapper;
import com.chat.pojo.User;
import com.chat.util.DbUtil;
import org.apache.ibatis.session.SqlSession;


public class Mybatis {
    public static void main(String[] args) {
        SqlSession session = DbUtil.getSession();
        UserMapper userMapper = session.getMapper(UserMapper.class);

        User user = new User();
        user.setUsername("lis");
        user.setPassword("123456");
        userMapper.addUser(user);
        System.out.println("用户添加成功！");
        session.commit();

    }
}


// public class Mybatis {
//     public static void main(String[] args) {
//         SqlSession session = null;
//         try {
//             session = DbUtil.getSession();
//             UserMapper userMapper = session.getMapper(UserMapper.class);
//             User user = new User();
//             user.setUsername("zhangsan");
//             user.setPassword("123456");
//
//             userMapper.addUser(user);
//             session.commit(); // 显式提交事务
//             System.out.println("用户添加成功！");
//         } catch (Exception e) {
//             if (session != null) {
//                 session.rollback(); // 出错时回滚
//             }
//             e.printStackTrace();
//         } finally {
//             if (session != null) {
//                 session.close(); // 关闭会话
//             }
//         }
//     }
// }
